"""
Case Type   : HTAP行列融合特性
Case Name   : 极限测试场景:行存表大于1000
Create At   : 2025/1/21
Owner       : ningyali
Description :
    1.建1000+行存表并插入数据
    2.进行全表转换
    3.查看query_imcstore_views()
    4.查询
    5.取消转换
    6.进行部分列转换
    7.查看query_imcstore_views()
    8.查询
    9.取消转换
    10.进行指定分区转换
    11.查看query_imcstore_views()
    12.查询
    13.取消转换
    14.进行指定分区部分列转换
    15.查看query_imcstore_views()
    16.查询
    17.清理数据环境
    18.查看query_imcstore_views()
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5.执行成功
    6.执行成功
    7.执行成功
    8.执行成功
    9.执行成功
    10.执行成功
    11.执行成功
    12.执行成功
    13.执行成功
    14.执行成功
    15.执行成功
    16.执行成功
    17.执行成功
    18.执行成功
History     :
"""

import os
import re
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.t_name = 't_htap_single_0158'
        self.max_num = 1001
        self.step = 100
        self.num_list = [list(range(1, self.max_num))[
                         i * self.step:i * self.step + self.step] for i in
                         range(self.max_num // self.step)]

    def test_trans(self):
        text = '----step1:建1000+行存表;expect:执行成功----'
        self.log.info(text)
        self.log.info('建表，避免建表语句过长报错Argument list too long')
        for i in self.num_list:
            create_sql = ""
            for j in i:
                create_sql += f'''
                drop table if exists {self.t_name}_{j};
                create table {self.t_name}_{j}(
                    c1 integer,
                    c2 varchar(30),
                    c3 char(30))
                partition by range (c1)
                (
                    partition {self.t_name}_{j}_p1 values less than (1000),
                    partition {self.t_name}_{j}_p2 values less than (2000),
                    partition {self.t_name}_{j}_p3 values less than (3000),
                    partition {self.t_name}_{j}_p4 values less than (maxvalue)
                );
                insert into {self.t_name}_{j} values (
                    generate_series(1, 10000),
                    generate_series(1, 10000)||'c22222222222',
                    generate_series(1, 10000)||'c33333333333');\n'''
            sql_res = self.pri_sh.execut_db_sql(
                create_sql, env_path=macro.SINGLE_DB_ENV_PATH)
            self.assertLessEqual(sql_res.count('CREATE TABLE'), self.step,
                                 f'建表失败:{text}')
            self.assertLessEqual(sql_res.count('INSERT 0 1'), self.step,
                                 f'插入失败:{text}')

        text = '----step2:全表转换; expect:执行成功----'
        self.log.info(text)
        sql_list = [f"alter table {self.t_name}_{i} imcstored;" for i in
                    range(1, self.max_num)]
        sql_res = self.pri_sh.execut_db_sql(
            '\n'.join(sql_list), env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), self.max_num - 1,
                         f'执行失败:{text}')

        text = '----step3:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('5000 rows', sql_res, f'执行失败:{text}')

        text = '----step4:查询; expect:执行成功----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(*) from {self.t_name}_1;' \
              f'select count(*) from {self.t_name}_1;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertIn('10000', sql_res, f'执行失败:{text}')

        text = '----step5:取消转换; expect:执行成功----'
        self.log.info(text)
        sql_list = [f"alter table {self.t_name}_{i} unimcstored;"
                    for i in range(1, self.max_num)]
        sql_res = self.pri_sh.execut_db_sql(
            '\n'.join(sql_list), env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), self.max_num - 1,
                         f'执行失败:{text}')

        text = '----step6:部分列转换; expect:执行成功----'
        self.log.info(text)
        sql_list = [f"alter table {self.t_name}_{i} imcstored(c1,c3);"
                    for i in range(1, self.max_num)]
        sql_res = self.pri_sh.execut_db_sql(
            '\n'.join(sql_list), env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), self.max_num - 1,
                         f'执行失败:{text}')

        text = '----step7:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('5000 rows', sql_res, f'执行失败:{text}')

        text = '----step8:查询; expect:执行成功----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(c3) ' \
              f'from {self.t_name}_100;' \
              f'select count(c3) from {self.t_name}_100;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertIn('10000', sql_res, f'执行失败:{text}')

        text = '----step9:取消转换; expect:执行成功----'
        self.log.info(text)
        sql_list = [f"alter table {self.t_name}_{i} unimcstored;"
                    for i in range(1, self.max_num)]
        sql_res = self.pri_sh.execut_db_sql(
            '\n'.join(sql_list), env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), self.max_num - 1,
                         f'执行失败:{text}')

        text = '----step10:指定分区转换; expect:执行成功----'
        self.log.info(text)
        sql_list = [f"alter table {self.t_name}_{i} modify partition " \
                    f"{self.t_name}_{i}_p4 imcstored;"
                    for i in range(1, self.max_num)]
        sql_res = self.pri_sh.execut_db_sql(
            '\n'.join(sql_list), env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), self.max_num - 1,
                         f'执行失败:{text}')

        text = '----step11:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('2000 rows', sql_res, f'执行失败:{text}')

        text = '----step12:查询; expect:执行成功----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(*) from {self.t_name}_89 ' \
              f'partition({self.t_name}_89_p4);' \
              f'select count(*) from {self.t_name}_89 ' \
              f'partition({self.t_name}_89_p4);'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertIn('7001', sql_res, f'执行失败:{text}')

        text = '----step13:取消转换; expect:执行成功----'
        self.log.info(text)
        sql_list = [f"alter table {self.t_name}_{i} modify partition " \
                    f"{self.t_name}_{i}_p4 unimcstored;"
                    for i in range(1, self.max_num)]
        sql_res = self.pri_sh.execut_db_sql(
            '\n'.join(sql_list), env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), self.max_num - 1,
                         f'执行失败:{text}')

        text = '----step14:指定分区部分列转换; expect:执行成功----'
        self.log.info(text)
        sql_list = [f"alter table {self.t_name}_{i} modify partition " \
                    f"{self.t_name}_{i}_p4 imcstored(c1,c3);"
                    for i in range(1, self.max_num)]
        sql_res = self.pri_sh.execut_db_sql(
            '\n'.join(sql_list), env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), self.max_num - 1,
                         f'执行失败:{text}')

        text = '----step15:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('2000 rows', sql_res, f'执行失败:{text}')

        text = '----step16:查询; expect:执行成功----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(c1) from ' \
              f'{self.t_name}_66 partition({self.t_name}_66_p4);' \
              f'select count(c1) from {self.t_name}_66 ' \
              f'partition({self.t_name}_66_p4);'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertIn('7001', sql_res, f'执行失败:{text}')

    def tearDown(self):
        text1 = '----step17:清理环境;expect:执行成功----'
        self.log.info(text1)
        sql_list = [f"drop table if exists {self.t_name}_{i};"
                    for i in range(1, self.max_num)]
        sql_res1 = self.pri_sh.execut_db_sql(
            '\n'.join(sql_list), env_path=macro.SINGLE_DB_ENV_PATH)

        text2 = '----step18:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text2)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res2 = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)

        self.assertEqual(sql_res1.count('DROP TABLE'), self.max_num - 1,
                         f'执行失败:{text1}')
        self.assertIn('0 rows', sql_res2,
                      f'删除表后未清理query_imcstore_views:{text2}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
